package com.kgc.scd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.scd.bean.ShopGoods;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.vo.ShopGoodsVO;
import com.kgc.scd.mapper.GoodsMapper;
import com.kgc.scd.service.GoodsService;
import com.kgc.scd.vo.ShopHotWordsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class GoodsServiceimpl implements GoodsService {
    @Autowired(required = false)
    private GoodsMapper goodsMapper;

    @Override
    public PageSupport<ShopGoodsVO> PageList(String words, Integer pageNo, Integer pageSize) {
        QueryWrapper<ShopGoods>queryWrapper=new QueryWrapper<>();
        queryWrapper.likeRight("name",words)
                    .or().likeRight("goods_brief",words);
        Page<ShopGoods>page=new Page<>(pageNo,pageSize);
        //总页数
        page.getPages();
        //总条数
        page.getTotal();
        Page<ShopGoods>shopGoodsPage=goodsMapper.selectPage(page,queryWrapper);
        List<ShopGoodsVO> shopGoodsPageList=shopGoodsPage.getRecords().stream().map(collect->{
            ShopGoodsVO shopGoodsVO=new ShopGoodsVO();

            BeanUtils.copyProperties(collect,shopGoodsVO);
            return  shopGoodsVO;
        }).collect(Collectors.toList());

       PageSupport<ShopGoodsVO>pageSupport=new PageSupport<>();
       pageSupport.setPageNo(pageNo);
       pageSupport.setPageSize(pageSize);
       pageSupport.setTotalPage((int) page.getPages());
       pageSupport.setTotalCount((int) page.getTotal());
        pageSupport.setPageData(shopGoodsPageList);

        return pageSupport;
    }
}
